<?php
namespace Home\Controller;

class CenterController extends HomeBaseController
{

    /**
     *  交易大厅
     */
    public function index()
    {
        if(session('id')==null){
            redirect(U('Login/index'));
            exit();
        }
        $user=M('UserAcc')->find(session('id'));
        $user['paswd']='';
        $this->assign('user',$user);
        $count = M('ReqkeyLog')->where("uid=".session('id'))->count();
        $this->assign('count',$count);
        $ugzhCount = M('UserGzh'.session('id'))->count();
        $this->assign('ugzhCount',$ugzhCount);
        $this->display('center/index');
    }
    
    public function update(){
        if(session('id')==null){
            redirect(U('Login/index'));
            exit();
        }
        $comp=I('comp','');
        $mobile=I('mobile','');
        $email=I('email','');
        $qq=I('qq','');
        if($comp!=''){
            M('UserAcc')->where('id='.session('id'))->setField(array('comp'=>$comp));
        }else if($mobile!=''){
            M('UserAcc')->where('id='.session('id'))->setField(array('mobile'=>$mobile));
        }else if($email!=''){
            M('UserAcc')->where('id='.session('id'))->setField(array('email'=>$email));
        }else if($qq!=''){
            M('UserAcc')->where('id='.session('id'))->setField(array('qq'=>$qq));
        }
        redirect(U('Center/index'));
    }
    
    
    public function mky()
    {
        if(session('id')==null){
            redirect(U('Login/index'));
            exit();
        }
        $user=M('UserAcc')->field('md5key')->find(session('id'));
        $this->assign('md5key',$user['md5key']);
        $this->display('center/mky');
    }
    
    public function updatekey(){
        if(session('id')==null){
            redirect(U('Login/index'));
            exit();
        }
        $key=I('key','');
        $qian=array(" ","　","\t","\n","\r");
        $hou=array("","","","","");
        $key=str_replace($qian,$hou,$key);
        if($key!='' && strlen($key)<=200){
            M('UserAcc')->where('id='.session('id'))->setField(array('md5key'=>$key));
        }
        redirect(U('Center/mky'));
    }
    
    
    public function wxreqlog()
    {
        if(session('id')==null){
            redirect(U('Login/index'));
            exit();
        }
        $condi['a.uid']=session('id');
        $page=I('page',0);
        $this->assign("page",$page);
        $size=20;
        $ReqkeyLog = M('ReqkeyLog');
        
        $count=$ReqkeyLog->join('as a left join wx_gzh as b on a.biz = b.biz')->where($condi)->count();
        $this->assign("count",$count);
        $pageSize=floor($count/$size);
        if($count%$size>0){
            $pageSize=$pageSize+1;
        }
        $start=0;
        if($page>0){
            $start=$page*$size;
        }
        
        $this->assign("pageSize",$pageSize);
        $list = $ReqkeyLog->join('as a left join wx_gzh as b on a.biz = b.biz')
        ->field('a.biz,a.createdate,b.name')->where($condi)->order("a.createdate desc")->limit($start,$size)->select();
        $this->assign('list',$list);
        $this->display('center/wxreglog');
    }
    
    
    public function wxgzhlist()
    {
        if(session('id')==null){
            redirect(U('Login/index'));
            exit();
        }
    
        $page=I('page',0);
        $this->assign("page",$page);
        $size=20;
        $Usergzh = M('UserGzh'.session('id'));
        $count=$Usergzh->join('as a left join wx_gzh as b on a.biz = b.biz')->count();
        $this->assign("count",$count);
        $pageSize=floor($count/$size);
        if($count%$size>0){
            $pageSize=$pageSize+1;
        }
        $start=0;
        if($page>0){
            $start=$page*$size;
        }
        $this->assign("pageSize",$pageSize);
        $list = $Usergzh->join('as a left join wx_gzh as b on a.biz = b.biz')
        ->field('a.msgid,a.biz,a.createdate,b.name')->order("a.createdate desc")->limit($start,$size)->select();
        $this->assign('list',$list);
        $this->display('center/gzhlist');
    }
    
    
    public function updateGzh()
    {
        if(session('id')==null){
            redirect(U('Login/index'));
            exit();
        }
        $gzhStrs=I('gzhStrs','');
        if($gzhStrs==''){
            redirect(U('Center/wxgzhlist'));
            exit();
        }
        $arr = explode('@',$gzhStrs);
        if(empty($arr) || count($arr)<2){
            redirect(U('Center/wxgzhlist'));
            exit();
        }
        $arr2 = array();
        foreach($arr as $v){
            $sonArr = explode(';',$v);
            if(!empty($sonArr) && count($sonArr)==2){
                $map=array('biz'=>$sonArr[0],'name'=>$sonArr[1]);
                array_push($arr2,$map);
            }
        }
        if(count($arr2)==0){
            redirect(U('Center/wxgzhlist'));
            exit();
        }
        $cztype=I('cztype','1');
        $qian=array(" ","　","\t","\n","\r");
        $hou=array("","","","","");
        if($cztype=='1' || $cztype==1){
            //新增
            foreach($arr2 as $obj){
                $biz=str_replace($qian,$hou,$obj['biz']);
                $name=str_replace($qian,$hou,$obj['name']);
                $count=M('UserGzh'.session('id'))->where("biz='".$biz."'")->count();
                if($count==0){
                    $ugzd['biz']=$biz;
                    $ugzd['createdate']=date("Y-m-d H:i:s ");
                    M('UserGzh'.session('id'))->add($ugzd);
                }
                $gzcount=M('Gzh')->where("biz='".$biz."'")->count();
                if($count==0){
                    $gzd['biz']=$biz;
                    $gzd['name']=$name;
                    $gzd['createdate']=date("Y-m-d H:i:s ");
                    M('Gzh')->add($gzd);
                }
            }
        }else{
            //更新-删除以前绑定的公众号
            M('UserGzh'.session('id'))->delete();
            foreach($arr2 as $obj){
                $biz=str_replace($qian,$hou,$obj['biz']);
                $name=str_replace($qian,$hou,$obj['name']);
                $ugzd['biz']=$biz;
                $ugzd['createdate']=date("Y-m-d H:i:s ");
                M('UserGzh'.session('id'))->add($ugzd);
                $gzcount=M('Gzh')->where("biz='".$biz."'")->count();
                if($count==0){
                    $gzd['biz']=$biz;
                    $gzd['name']=$name;
                    $gzd['createdate']=date("Y-m-d H:i:s ");
                    M('Gzh')->add($gzd);
                }
            }
        }
        redirect(U('Center/wxgzhlist'));
    }
    
    public function openserv()
    {
        if(session('id')==null){
            redirect(U('Login/index'));
            exit();
        }
        $user = M('UserAcc')->find(session('id'));
        if(empty($user)){
            redirect(U('Login/index'));
            exit();
        }
        if($user['protype']>1){
            redirect(U('Center/index'));
            exit();
        }
        M('UserAcc')->where('id='.session('id'))->setField(array('protype'=>1));
        session('protype', 1);
        redirect(U('Center/wxgzhlist'));
    }
    
    
    
    
    public function workorders()
    {
        if(session('id')==null){
            redirect(U('Login/index'));
            exit();
        }
    
        $condi['uid']=session('id');
        $page=I('page',0);
        $this->assign("page",$page);
        $size=20;
        $count=M('Workorder')->where($condi)->count();
        $this->assign("count",$count);
        $pageSize=floor($count/$size);
        if($count%$size>0){
            $pageSize=$pageSize+1;
        }
        $start=0;
        if($page>0){
            $start=$page*$size;
        }
        $this->assign("pageSize",$pageSize);
        $list=M('Workorder')->field("id,title,createdate")->where($condi)->order('createdate desc')->limit($start,$size)->select();
        $this->assign('list',$list);
        $this->display('center/workorders');
    }
    
    public function saveworkorder(){
        if(session('id')==null){
            redirect(U('Login/index'));
            exit();
        }
       $title = I('title','');
       $descr = I('descr','');
       if($title==''){
           redirect(U('Center/workorders'));
           exit();
       }
       $data['title']=$title;
       $data['uid']=session('id');
       $data['descip']=$descr;
       $data['createdate']=date("Y-m-d H:i:s ");
       $order = M('Workorder');
       $order->add($data);
       redirect(U('Center/workorders'));
    }
    
    public function workposts(){
        if(session('id')==null){
            redirect(U('Login/index'));
            exit();
        }
        $condi['orderid']=I('orderid','');
        $list=M('WorkPost')->field("id,descrip,orderid,uid,createdate")->where($condi)->order('createdate')->select();
        $this->assign('list',$list);
        $order = M('Workorder')->field("id,title,descip")->find(I('orderid',''));
        $this->assign('order',$order);
        $this->display('center/workposts');
    }
    
    public function saveworkpost(){
        if(session('id')==null){
            redirect(U('Login/index'));
            exit();
        }
        $descr = I('descr','');
        $orderid = I('orderid','');
        if($descr=='' || orderid==''){
            redirect(U('Center/workposts'));
            exit();
        }
        $data['orderid']=$orderid;
        $data['uid']=session('id');
        $data['descrip']=$descr;
        $data['createdate']=date("Y-m-d H:i:s ");
        $order = M('WorkPost');
        $order->add($data);
        $this->redirect('Center/workposts', array('orderid' =>$orderid),1, '回复已提交...');
    }
    
}